﻿using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZipFile = System.IO.Compression.ZipFile;

namespace FieldTools.GetGiteeSoftPublish
{
    /// <summary>
    /// zip压缩工具类，不完善，可以直接使用System.IO.Compression中的方法
    /// </summary>
    public static class ZipTool
    {
        /// <summary>
        /// 创建zip压缩文件
        /// </summary>
        /// <param name="sourceDirectoryName">要压缩的文件夹</param>
        /// <param name="destinationArchiveFileName">压缩后的文件名</param>
        public static void CreateFromDirectory(string sourceDirectoryName,string destinationArchiveFileName)
        {
            try
            {
                ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 创建zip压缩文件
        /// </summary>
        /// <param name="sourceDirectoryName">要压缩的文件夹</param>
        /// <param name="destinationArchiveFileName">压缩后的文件名</param>
        /// <param name="compressionLevel">压缩级别  Fastest-最快 NoCompression-不压缩 Optimal-最好</param>
        /// <param name="includeBaseDirectory">是否包含当前目录</param>
        public static void CreateFromDirectory(
             string sourceDirectoryName,//sourceDirectoryName 要压缩的文件夹
             string destinationArchiveFileName, //destinationArchiveFileName 压缩后的文件名
             CompressionLevel compressionLevel,//compressionLevel 压缩级别  Fastest-最快 NoCompression-不压缩 Optimal-最好
             bool includeBaseDirectory //includeBaseDirectory 是否包含当前目录
         )
        {
            try
            {
                ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, includeBaseDirectory);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 解压zip压缩文件
        /// </summary>
        /// <param name="sourceArchiveFileName">压缩文件完整路径</param>
        /// <param name="destinationDirectoryName">指定解压文件夹</param>
        public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName)
        {
            try
            {
                ZipFile.ExtractToDirectory(sourceArchiveFileName, destinationDirectoryName, Encoding.GetEncoding("GBK"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
